package com.neusoft.yuejian_6.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.neusoft.yuejian_6.R;


public class HomeFragment extends Fragment {
    //声明变量
    private View view=null;
    private ImageView search;//搜索
    private RollPagerView rollPV = null;

    private HomeJingxuanFragment activityFragment;
    private HomeZuijinFragment contestFragment;
    private HomeQuanweiFragment masterFragment;

    private RadioButton activityRadio;
    private RadioButton masterRadio;
    private RadioButton contestRadio;

    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    public HomeFragment(){}

    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view= inflater.inflate(R.layout.fragment_home, container, false);

        initViews();
        initEvents();
        return view;
    }

    public void initViews(){
        //search=(ImageView)view.findViewById(R.id.search);
        rollPV = (RollPagerView) view.findViewById(R.id.rollPV);
    }

    public void initEvents(){

        //跳转到搜索页
        /*search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), HomeSearchActivity.class);
                startActivityForResult(intent, 1);
            }
        });*/


        /****************轮播图*****************/
        rollPV.setPlayDelay(3000);
        //设置图片切换动画时间
        rollPV.setAnimationDurtion(500);
        //设置指示器:
        //rollPV.setHintView(new IconHintView());
        //rollPV.setHintView(new IconHintView(this,R.mipmap.ic_launcher,R.mipmap.ic_launcher));
        rollPV.setHintView(new ColorPointHintView(getActivity(), getResources().getColor(R.color.colorPrimary),
                Color.WHITE));
        //设置适配器
        rollPV.setAdapter(new RollPagerAdapter());

        //设置每一个图片的点击事件
        /*rollPV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(getActivity(), "点击了图片"+position, Toast.LENGTH_SHORT).show();

            }
        });*/


    }
    private int images[] = {
            R.drawable.go,
            R.drawable.login,
            R.drawable.jjjjj
    };

    private class RollPagerAdapter extends StaticPagerAdapter {

        @Override
        public View getView(ViewGroup container, int position) {

            ImageView view=new ImageView(container.getContext());
            //设置图片资源
            view.setImageResource(images[position]);
            //设置高度和宽度
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置拉伸方式
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);

            return view;
        }

        @Override
        public int getCount() {
            return images.length;
        }
    }

    //精选活动，近期比赛，权威大师等fragment
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);
        activityRadio = (RadioButton) view.findViewById(R.id.jingxuanActivity);
        masterRadio = (RadioButton) view.findViewById(R.id.quanweiMaster);
        contestRadio = (RadioButton) view.findViewById(R.id.zuijinContest);

        activityRadio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    activityFragment = new HomeJingxuanFragment();
                    replaceFragment(activityFragment);
                }
            }
        });

        contestRadio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    contestFragment = new HomeZuijinFragment();
                    replaceFragment(contestFragment);
                }
            }
        });

        masterRadio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    masterFragment = new HomeQuanweiFragment();
                    replaceFragment(masterFragment);
                }
            }
        });


        fragmentManager=getChildFragmentManager();
        activityRadio.setChecked(true);

    }
    private void replaceFragment(Fragment fragment){
        transaction=fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_container,fragment);
        transaction.commit();
    }
}
